package middleware

import (
	"github.com/gin-gonic/gin"
	"interview/pkg/common"
	"interview/pkg/global"
	"interview/pkg/response"
	"net/http"
	"runtime/debug"
)

func Exception(c *gin.Context) {
	defer func() {

		if err := recover(); err != nil {
			// 记录异常日志
			global.Logger.Errorf("未知异常: %v\n堆栈信息: %v", err, string(debug.Stack()))

			c.JSON(http.StatusOK, response.Response{
				Code: common.InternalServerError,
				Data: map[string]interface{}{},
				Msg:  common.InternalServerErrorMsg,
			})
			c.Abort()
			return
		}
	}()

	c.Next()
}
